package com.yunfeisoft.business.dao.impl;

import com.yunfeisoft.base.model.User;
import com.yunfeisoft.business.dao.inter.OutHospitalApprovalResultDao;
import com.yunfeisoft.business.model.OutHospitalApprovalResult;
import com.yunfeisoft.common.base.BaseDaoImpl;
import com.yunfeisoft.common.sql.builder.SelectBuilder;
import com.yunfeisoft.common.sql.builder.WhereBuilder;
import com.yunfeisoft.common.utils.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * ClassName: OutHospitalApprovalResultDaoImpl
 * Description: 老人出院审批记录表Dao实现
 * Author: Jackie liu
 * Date: 2020-05-13
 */
@Repository
public class OutHospitalApprovalResultDaoImpl extends BaseDaoImpl<OutHospitalApprovalResult, String> implements OutHospitalApprovalResultDao {

    @Override
    public Page<OutHospitalApprovalResult> queryPage(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        if (params != null) {
            initPageParam(wb, params);
        }
        return queryPage(wb);
    }

    /**
     * 根据老人id查询出院审批记录列表
     * @param outId
     * @return
     */
    @Override
    public List<OutHospitalApprovalResult> queryListByOutId(String outId) {
        if (StringUtils.isBlank(outId)) {
            return new ArrayList<>();
        }
        WhereBuilder whereBuilder = new WhereBuilder();
        // 按照创建时间倒序
        whereBuilder.setOrderByWithDesc("createTime");
        whereBuilder.andEquals("outHospitalId", outId);
        SelectBuilder builder = getSelectBuilder("o");
        builder.column("u.name as username")
                .leftJoin(User.class)
                .alias("u")
                .on("u.id = o.createId").build();
        return query(builder.getSql(), whereBuilder);
    }
}